// selectwalkaider.cpp : implementation file
//

#include "stdafx.h"
#include "../Frame/GYWalkAidApp.h"
#include "SelectWalkAider.h"
#include "../Bluetooth/CWAManager.h"
#include "../Frame/NavigateDlg.h"
#include "../Common/CommonDef.h"
#include "../Common/GYDataSet.h"
#include "../Common/CommonFun.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define		COMBO_MASK_COLOR		RGB(128, 128, 128)

/////////////////////////////////////////////////////////////////////////////
// CSelectWalkAider dialog

static struct 
{
	DWORD			rdoID;
	DWORD			btnBeepID;
	DWORD			cboColorID;
	int				nWAIndex;
	DWORD			dwCommVersion;
	DWORD			aBattery[5];
}
s_Contrls[] =
{
	{IDC_RADIO1,	IDC_BUTTON_BEEP1,	IDC_COMBO_COLOR1,	-1,		0,
	{IDC_STATIC_BATTERY00,	IDC_STATIC_BATTERY01,	IDC_STATIC_BATTERY02,	IDC_STATIC_BATTERY03,	IDC_STATIC_BATTERY04 }}, 

	{IDC_RADIO2,	IDC_BUTTON_BEEP2,	IDC_COMBO_COLOR2,	-1,		0,
	{IDC_STATIC_BATTERY10,	IDC_STATIC_BATTERY11,	IDC_STATIC_BATTERY12,	IDC_STATIC_BATTERY13,	IDC_STATIC_BATTERY14 }}, 

	{IDC_RADIO3,	IDC_BUTTON_BEEP3,	IDC_COMBO_COLOR3,	-1,		0,
	{IDC_STATIC_BATTERY20,	IDC_STATIC_BATTERY21,	IDC_STATIC_BATTERY22,	IDC_STATIC_BATTERY23,	IDC_STATIC_BATTERY24 }}, 

	{IDC_RADIO4,	IDC_BUTTON_BEEP4,	IDC_COMBO_COLOR4,	-1,		0,
	{IDC_STATIC_BATTERY30,	IDC_STATIC_BATTERY31,	IDC_STATIC_BATTERY32,	IDC_STATIC_BATTERY33,	IDC_STATIC_BATTERY34 }}, 

	{IDC_RADIO5,	IDC_BUTTON_BEEP5,	IDC_COMBO_COLOR5,	-1,		0,
	{IDC_STATIC_BATTERY40,	IDC_STATIC_BATTERY41,	IDC_STATIC_BATTERY42,	IDC_STATIC_BATTERY43,	IDC_STATIC_BATTERY44 }}, 
};



CSelectWalkAider::CSelectWalkAider(CWnd* pParent /*=NULL*/)
	: CDialog(CSelectWalkAider::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSelectWalkAider)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CSelectWalkAider::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSelectWalkAider)
	DDX_Control(pDX, IDC_COMBO_COLOR5, m_cmbColor5);
	DDX_Control(pDX, IDC_COMBO_COLOR4, m_cmbColor4);
	DDX_Control(pDX, IDC_COMBO_COLOR3, m_cmbColor3);
	DDX_Control(pDX, IDC_COMBO_COLOR2, m_cmbColor2);
	DDX_Control(pDX, IDC_COMBO_COLOR1, m_cmbColor1);
	DDX_Control(pDX, IDC_BUTTON_BEEP5, m_btnBeep5);
	DDX_Control(pDX, IDC_BUTTON_BEEP4, m_btnBeep4);
	DDX_Control(pDX, IDC_BUTTON_BEEP3, m_btnBeep3);
	DDX_Control(pDX, IDC_BUTTON_BEEP2, m_btnBeep2);
	DDX_Control(pDX, IDC_BUTTON_BEEP1, m_btnBeep1);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSelectWalkAider, CDialog)
	//{{AFX_MSG_MAP(CSelectWalkAider)
	ON_BN_CLICKED(IDC_BUTTON_CANCEL, OnButtonCancel)
	ON_BN_CLICKED(IDC_BUTTON_OK, OnButtonOK)
	ON_BN_CLICKED(IDC_RADIO1, OnRadioClicked)
	ON_BN_CLICKED(IDC_BUTTON_SEARCH_WA, OnButtonSearchWa)
	ON_BN_CLICKED(IDC_BUTTON_BEEP1, OnButtonBeep1)
	ON_BN_CLICKED(IDC_BUTTON_BEEP2, OnButtonBeep2)
	ON_BN_CLICKED(IDC_BUTTON_BEEP3, OnButtonBeep3)
	ON_BN_CLICKED(IDC_BUTTON_BEEP4, OnButtonBeep4)
	ON_BN_CLICKED(IDC_RADIO2, OnRadioClicked)
	ON_BN_CLICKED(IDC_RADIO3, OnRadioClicked)
	ON_BN_CLICKED(IDC_RADIO4, OnRadioClicked)
	ON_BN_CLICKED(IDC_RADIO5, OnRadioClicked)
	ON_BN_CLICKED(IDC_BUTTON_BEEP5, OnButtonBeep5)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSelectWalkAider message handlers

void CSelectWalkAider::OnButtonCancel() 
{
	EndDialog(IDCANCEL);	
}

void CSelectWalkAider::OnButtonOK()
{
	BOOL bDuplicated = FALSE;
	int anColorCount[MAX_SUPPORT_WA_COUNT] = {0};

	for(int i = 0; i < ARRAY_ELE_COUNT(s_Contrls); i++)
	{
		CComboBox* pComboColor = (CComboBox*)GetDlgItem(s_Contrls[i].cboColorID);
		ASSERT(pComboColor);
		
		if(pComboColor->IsWindowVisible())
		{
			int nSel = pComboColor->GetCurSel();
			if(nSel != CB_ERR && nSel < MAX_SUPPORT_WA_COUNT)
			{
				if(anColorCount[nSel] != 0)
				{
					bDuplicated = TRUE;
					break;
				}
				else
				{
					anColorCount[nSel] = 1;
				}
			}
		}
	}

	if(bDuplicated)
	{
		if(GYMsgBox(IDS_PAT_DUP_WA_COLOR, MB_ICONQUESTION|MB_YESNO) != IDYES)
			return;
	}

	for(i = 0; i < ARRAY_ELE_COUNT(s_Contrls); i++)
	{
		CButton* pRadio = (CButton*)GetDlgItem(s_Contrls[i].rdoID);
		if(pRadio->GetCheck())
		{
			GetApp()->SetCurrentWAIndex(s_Contrls[i].nWAIndex);
			break;
		}
	}

	SaveWAColor();
	EndDialog(IDOK);
}

void CSelectWalkAider::OnRadioClicked() 
{
	GetDlgItem(IDC_BUTTON_OK)->EnableWindow(TRUE);	
}

BOOL CSelectWalkAider::OnInitDialog() 
{
	CDialog::OnInitDialog();

	InitWAColorData();
	InitBeepButton();
	InitWAList();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSelectWalkAider::InitWAList()
{
	HideAllWACtrls();

	CWAManager* pBTMan = GetApp()->GetWAMan();
	if(!pBTMan)	return;

	//  The current WA
	int nCurrentPos = 0;
	for(int nIndex = pBTMan->GetFirstDevIndex(); nIndex >= 0; nIndex = pBTMan->GetNextDevIndex(nIndex))
	{
		CString strDevID = pBTMan->GetWADevID(nIndex);
		if(pBTMan->IsWAConnected(nIndex) && !strDevID.IsEmpty())
		{
			CWnd* pRadio	= GetDlgItem(s_Contrls[nCurrentPos].rdoID);
			pRadio->ShowWindow(SW_SHOW);
			pRadio->SetWindowText(strDevID);

			DWORD dwCommVer = pBTMan->GetWACommVersion(nIndex);
			s_Contrls[nCurrentPos].dwCommVersion = dwCommVer;
			s_Contrls[nCurrentPos].nWAIndex = nIndex;

			if(dwCommVer >= 0x12)
			{
				//  Beep button
				CWnd* pBtnBeep	= GetDlgItem(s_Contrls[nCurrentPos].btnBeepID);
				pBtnBeep->ShowWindow(SW_SHOW);

				//  Battery
				DWORD dwBattery		= pBTMan->cmdGetBattery(nIndex);
				CWnd* pBattery		= GetDlgItem(s_Contrls[nCurrentPos].aBattery[dwBattery]);
				pBattery->ShowWindow(SW_SHOW);

				//  Color
				CString strBTAddress = pBTMan->GetWAAddress(nIndex);
				int nColorIndex = GetColorIndexFromAddress(strBTAddress);
				if(nColorIndex < 0)
					nColorIndex = 0;

				CComboBoxEx* pCombo = (CComboBoxEx*)GetDlgItem(s_Contrls[nCurrentPos].cboColorID);
				pCombo->ShowWindow(SW_SHOW);
				pCombo->SetCurSel(nColorIndex);
			}
			
			nCurrentPos++;
			if(nCurrentPos >= MAX_SUPPORT_WA_COUNT)
				break;
		}
	}
}

void CSelectWalkAider::OnButtonSearchWa() 
{
	CWAManager* pBTMan = GetApp()->GetWAMan();
	pBTMan->SearchWAImmediately();
	
}

void CSelectWalkAider::OnButtonBeep1() 
{
	BeepWA(0);
}

void CSelectWalkAider::OnButtonBeep2() 
{
	BeepWA(1);
}

void CSelectWalkAider::OnButtonBeep3() 
{
	BeepWA(2);
}

void CSelectWalkAider::OnButtonBeep4() 
{
	BeepWA(3);
}

void CSelectWalkAider::OnButtonBeep5() 
{
	BeepWA(4);
}

void CSelectWalkAider::BeepWA(int nIndex)
{
	ASSERT(nIndex >= 0 && nIndex < 5);

	if(s_Contrls[nIndex].nWAIndex >= 0)
	{
		CWAManager* pBTMan = GetApp()->GetWAMan();
		pBTMan->cmdReadWADevID(s_Contrls[nIndex].nWAIndex);
	}
}

void CSelectWalkAider::InitBeepButton()
{
	m_btnBeep1.LoadBitmaps(IDB_BITMAP_BEEP_NORMAL, IDB_BITMAP_BEEP, IDB_BITMAP_BEEP_NORMAL, IDB_BITMAP_BEEP);
	m_btnBeep1.SizeToContent();
	
	m_btnBeep2.LoadBitmaps(IDB_BITMAP_BEEP_NORMAL, IDB_BITMAP_BEEP, IDB_BITMAP_BEEP_NORMAL, IDB_BITMAP_BEEP);
	m_btnBeep2.SizeToContent();
	
	m_btnBeep3.LoadBitmaps(IDB_BITMAP_BEEP_NORMAL, IDB_BITMAP_BEEP, IDB_BITMAP_BEEP_NORMAL, IDB_BITMAP_BEEP);
	m_btnBeep3.SizeToContent();
	
	m_btnBeep4.LoadBitmaps(IDB_BITMAP_BEEP_NORMAL, IDB_BITMAP_BEEP, IDB_BITMAP_BEEP_NORMAL, IDB_BITMAP_BEEP);
	m_btnBeep4.SizeToContent();
	
	m_btnBeep5.LoadBitmaps(IDB_BITMAP_BEEP_NORMAL, IDB_BITMAP_BEEP, IDB_BITMAP_BEEP_NORMAL, IDB_BITMAP_BEEP);
	m_btnBeep5.SizeToContent();
}

void CSelectWalkAider::InitWAColorData()
{
	//  read WA color from table
	CString strSQL = _T("SELECT WAAddress, ColorIndex FROM WAColor");
	
	for(CGYDataSet setWAColor(strSQL); !setWAColor.IsEOF(); setWAColor.MoveNext())
	{
		CString strWAAddress, strColorIndex;
		setWAColor.GetFieldValue(_T("WAAddress"), strWAAddress);
		setWAColor.GetFieldValue(_T("ColorIndex"), strColorIndex);
		
		int nColorIndex = _ttoi(strColorIndex);
		nColorIndex = min(nColorIndex, 4);
		nColorIndex = max(nColorIndex, 0);

		if(strWAAddress.GetLength() == BT_ADDRESS_STRING_LEN && nColorIndex >= 0 && nColorIndex < MAX_SUPPORT_WA_COUNT)
		{
			WACOLOR wac = {0};
			_tcscpy(wac.szBTAddress, strWAAddress);
			wac.dwColorIndex = nColorIndex;

			m_vecWAColor.push_back(wac);
		}
		else
		{
			ASSERT(FALSE);
		}
	}
}

BOOL CSelectWalkAider::SaveWAColor()
{
	CWAManager* pBTMan = GetApp()->GetWAMan();

	for(int i = 0; i < ARRAY_ELE_COUNT(s_Contrls); i++)
	{
		CWnd* pRadio = GetDlgItem(s_Contrls[i].rdoID);
		ASSERT(pRadio);

		if(pRadio->IsWindowVisible() && s_Contrls[i].dwCommVersion >= 0x12)
		{
			CComboBox* pComboColor = (CComboBox*)GetDlgItem(s_Contrls[i].cboColorID);
			ASSERT(pComboColor);

			CString strAddress = pBTMan->GetWAAddress(s_Contrls[i].nWAIndex);
			int nSel = pComboColor->GetCurSel();

			CString strSQL;
			strSQL.Format(_T("SELECT ColorIndex FROM WAColor WHERE WAAddress='%s'"), strAddress);
			CGYDataSet setWAColor(strSQL);
			if(setWAColor.IsEOF())
			{
				strSQL.Format(_T("INSERT INTO WAColor (WAAddress, ColorIndex) VALUES ('%s', %d)"), strAddress, nSel);
				setWAColor.ExecuteSQL(strSQL);
			}
			else
			{
				CString strValue;
				setWAColor.GetFieldValue(_T("ColorIndex"), strValue);

				if(_ttoi(strValue) != nSel)
				{
					strSQL.Format(_T("UPDATE WAColor SET ColorIndex=%d WHERE WAAddress='%s'"), nSel, strAddress);
					setWAColor.ExecuteSQL(strSQL);
				}
			}
		}
	}

	return TRUE;
}

void CSelectWalkAider::HideAllWACtrls()
{
	//  Hide all WA controls first
	for(int k = 0; k < ARRAY_ELE_COUNT(s_Contrls); k++)
	{
		CWnd* pRadio	= GetDlgItem(s_Contrls[k].rdoID);
		CWnd* pCmbColor	= GetDlgItem(s_Contrls[k].cboColorID);
		CWnd* pBtnBeep	= GetDlgItem(s_Contrls[k].btnBeepID);
		
		pRadio->ShowWindow(SW_HIDE);
		pBtnBeep->ShowWindow(SW_HIDE);
		pCmbColor->ShowWindow(SW_HIDE);
		
		for(int j = 0; j < 5; j++)
		{
			CWnd* pBattery = GetDlgItem(s_Contrls[k].aBattery[j]);
			pBattery->ShowWindow(SW_HIDE);
		}
	}

}

int CSelectWalkAider::GetColorIndexFromAddress(CString strAddress)
{
	int nCorlorIndex = -1;
	for(int i = 0; i < m_vecWAColor.size(); i++)
	{
		if(strAddress.CompareNoCase(m_vecWAColor[i].szBTAddress) == 0)
		{
			nCorlorIndex = m_vecWAColor[i].dwColorIndex;

			nCorlorIndex = max(nCorlorIndex, 0);
			nCorlorIndex = min(nCorlorIndex, 4);
			break;
		}
	}

	return nCorlorIndex;
}